package com.cskaoyan.catmall.service.Impl;

import com.cskaoyan.catmall.dao.WxTopicMapper;
import com.cskaoyan.catmall.model.vo.*;
import com.cskaoyan.catmall.service.WxTopicService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建日期: 2022-01-09-22-20
 * author : XGQ
 * description: TopicService的实现类
 */
@Component
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRES_NEW)
public class WxTopicServiceImpl implements WxTopicService {
    @Autowired
    WxTopicMapper wxTopicMapper;

    /**
     * @param
     * @param id
     * @return com.cskaoyan.catmall.model.vo.TopicDetailVo
     * @description 专题商品实现
     * @author XGQ
     * @date 2022/01/10 9:23
     */
    @Override
    public TopicDetailVo getDetailById(Integer id) {
        // 获取topic变量参数
        TopicVo topic = wxTopicMapper.getTopicsById(id);

        // 获取goods变量参数
        List<TopicGoodsVo> goods = new ArrayList<>();
        //获取商品id
        Integer[] goodsIds = topic.getGoods();
        // 遍历商品id，通过id查找商品详情
        for (Integer goodsId : goodsIds) {
            TopicGoodsVo good = wxTopicMapper.getGoodsById(goodsId);
            goods.add(good);
        }
        //包装
        TopicDetailVo topicDetailVo = new TopicDetailVo();
        topicDetailVo.setGoods(goods);
        topicDetailVo.setTopic(topic);
        return topicDetailVo;
    }

    /**
     * @param
     * @param id
     * @return com.cskaoyan.catmall.model.vo.TopicRelatedVo
     * @description 专题相关产品连接
     * @author XGQ
     * @date 2022/01/10 9:24
     */
    @Override
    public TopicRelatedVo getRelatedById(Integer id) {
//        List<TopicVo> list = new ArrayList<>();
        Integer total = wxTopicMapper.getAllByDeleted();
        //给定limit，page
        // 条数限制
        Integer limit = 4;
        // 要获得数据
        Integer limit2 = limit + 1;
        Integer page = 1;
        Integer start = (page - 1) * limit2;
        // 获取数据
        List<TopicVo> list = wxTopicMapper.getTopicById(start, limit2);
        // 移除id的数据
        for (TopicVo topicVo : list) {
            if (topicVo.getId() == id){
                list.remove(topicVo);
                break;
            }
        }

        // 包装
        TopicRelatedVo topicRelatedVo = new TopicRelatedVo();
        topicRelatedVo.setLimit(limit);
        topicRelatedVo.setPages(total / limit + 1);
        topicRelatedVo.setPage(page);
        topicRelatedVo.setTotal(total);
        topicRelatedVo.setList(list);
        return topicRelatedVo;
    }

    /**
     * @param
     * @param page  第几页
     * @param limit 一页多少条
     * @return com.cskaoyan.catmall.model.vo.TopicListVo
     * @description 获取专题列表
     * @author XGQ
     * @date 2022/01/10 23:48
     */
    @Override
    public TopicListVo getTopicList(Integer page, Integer limit) {
        //分页，下标
        Integer start = (page - 1) * limit;
        // 根据下标获得相关的数据
        List<TopicVo> list = wxTopicMapper.getListByPageAndLimit(start, limit);
        // 获取未删除的条数
        Integer total = wxTopicMapper.getAllByDeleted();
        TopicListVo topicListVo = new TopicListVo();
        topicListVo.setLimit(limit);
        topicListVo.setPage(page);
        topicListVo.setPages(total / limit + 1);
        topicListVo.setTotal(total);
        topicListVo.setList(list);
        return topicListVo;
    }


}
